<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\index\controller;

use app\common\controller\Common;
use EasyWeChat\Foundation\Application;
use think\Db;

/**
 * 前台公共控制器
 * @package app\index\controller
 */
class Home extends Common
{
    public $openId;
    /**
     * 初始化方法
     * @author 蔡伟明 <314013107@qq.com>
     */
    public function _initialize()
    {
//        return 111;
        $app = new Application(config("wx"));
        $oauth = $app->oauth;

// 未登录
        if (!session('wechat_user')) {
            session("target_url",$this->request->url());
            // 这里不一定是return，如果你的框架action不是返回内容的话你就得使用
           $oauth->redirect()->send();
           exit();
        }
        $user=session("wechat_user");
//        halt($user);
        //获取open_id
        $this->openId=session("wechat_user")['id'];
        $member=Db::name("admin_member")->where("open_id",$this->openId)->find();
        //如果没有用户 就添加
        $name=$user['name'];
        $address=$user['original']['province'].$user['original']['city'];
        $logo=$user['avatar'];
        if ($member==null){
            $data=[
              "name"=>$name,
                "logo"=>$logo,
                "address"=>$address,
                "open_id"=>$user['id'],
                "jifen"=>0

            ];
            Db::name("admin_member")->insert($data);


        }


    }
}
